#!/usr/bin/env groovy
@Library('CI_Script')
import com.yidao.www.*;

def loraCamera = [
    [
        'name' : 'LoraBackend',
        'branch' : 'master'
    ],
    [
        'name' : 'LoraFrontend',
        'branch' : 'master'
    ],
    [
        'name' : 'LoraGatewayBase',
        'branch' : 'master'
    ],
    [
        'name' : 'LoraGatewayBsp',
        'branch' : 'master'
    ]
]

def embed = [
    [
        'name' : 'D200_LORA_CAMERA',
        'branch' : 'master'
    ]
]

def builds = [:]
def loraCameraBuild = new loraCameraBuild()
def embedBuild = new embedBuild()
def base = new base()
def packageName = params.PACKAGENAME
def majorVersion = params.MAJORVERSION
def subVersion = params.SUBVERSION
def stageVersion = params.STAGEVERSION
def date = new Date()
def wholePackageName = packageName + '_' + majorVersion + '.' + subVersion + '.' + stageVersion + '.tar'
def downloads = []
def localDir = './'
// def localPackage = localDir + '/' + wholePackageName
def artifactory = 'D200Robot/LoraCamera/Release'
def branch = 'master'

println '版本号:' + wholePackageName

for(i=0; i<loraCamera.size(); i++){
    def project = loraCamera[i]
    builds["${project.name}"] = {
        def result = loraCameraBuild.autoBuild(project.name, project.branch)
        downloads.add(result)
    }
}

for(i=0; i<embed.size(); i++){
    def project = embed[i]
    builds["${project.name}"] = {
        def result = embedBuild.autoBuild(project.name, project.branch)
        // downloads.add(result)
    }
}

timestamps{
    parallel builds
    println downloads
    def server = Artifactory.server 'jfrog-artifactory'
    def downloadUrl
    def srverUrl = server.url

    node(){
        for(download in downloads){
            println download
            downloadUrl = download
            println downloadUrl
            base.fileDownload(downloadUrl, localDir)
        }
        sh "tar cvf ${wholePackageName} *"

        def deployTarget = base.backupFolder(artifactory, packageName, branch, date)
        base.fileUpload(wholePackageName, deployTarget)
        cleanWs()
        def storageURL = srverUrl + '/' + deployTarget
        currentBuild.description = """<span>存储地址:<a href=${storageURL}>${storageURL}</a></span>"""
    }
}